#include <strings.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/syscall.h>

#include "FileInfo.h"
#include "Global.h"

typedef struct 
{
    char suffix[20];
    char ContentType[50];
}FileType;

typedef struct
{
    char suffix[20];
    char filetype[FILETYPELEN];
}FileSuffixType;

FileSuffixType FileSuffixType_Table[]=
{
    {".html",	"text/html; charset=utf-8"},
    {".png",	"image/png"},
    {".jpg",	"image/jepg"},
    {".js",		"application/x-javascript"},
    {".css",	"text/css"},
    {".gif",	"image/gif"},
    {".ico",	"image/x-icon"},
};

int FileSuffixType_TableSize = sizeof(FileSuffixType_Table) / sizeof(FileSuffixType);

FileInfo::FileInfo(char *Path)
{
    bzero(m_FileType, FILETYPELEN);
    /* get file size */
    struct stat statbuff;
    if(-1 != stat(Path, &statbuff))
    {  
        m_fileSize = statbuff.st_size;
    }

    /* get file type */
    int len;
    char *ret;
    char suffix[10];
    bzero(suffix, 10);
    ret = strrchr(Path, '.');  //get file suffix
    //DEBUG_LOG("ret=%s",ret);
    int index = 0;
    if(ret == NULL)
    {
        strcpy(m_FileType, "application/octet-stream") ; //Binary file
    }
    else
    {
        strcpy(suffix, ret);
        for(index = 0; index < FileSuffixType_TableSize; index++)
        {
        	if(0 == strcmp(suffix, FileSuffixType_Table[index].suffix))
        	{
        		strcpy(m_FileType, FileSuffixType_Table[index].filetype);
        		break;
        	}
        }
    }
   // DEBUG_LOG("index=%d",index);
}
FileInfo::~FileInfo()
{
    //do nothing
}

char *
FileInfo::getFileType()
{
    return m_FileType;
}

unsigned long
FileInfo::getFileSize()
{
    return m_fileSize;
}
